package com.daye.homework.servlet;

import com.daye.homework.bean.Class;
import com.daye.homework.bean.Student;
import com.daye.homework.factory.AdminServiceFactory;
import com.daye.homework.service.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "SendUpdateClassServlet" , urlPatterns = "/sendUpdateClass")
public class SendUpdateClassServlet extends HttpServlet {
    private AdminService adminService = AdminServiceFactory.getAdminService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //班级id
        int id = Integer.parseInt(request.getParameter("id"));
        //班级名称
        String className = request.getParameter("className");
        //年级
        Integer grade = Integer.parseInt(request.getParameter("grade"));
        //教师名称
        String teacherName = request.getParameter("teacherName");
        //班级口号
        String catchword = request.getParameter("catchword");


        Class myClass = new Class(className , grade , teacherName , catchword);
        myClass.setId(id);

        try {
            adminService.updateClassById(myClass);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //跳回遍历所有班级
        response.sendRedirect("queryClass");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
